<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button
          v-auth="['documentManager:tag:create']"
          type="primary"
          preIcon="ion:add-outline"
          @click="handleAdd"
          >{{ t('routes.demo.knowledgeManager.labelManagerSet.create') }}</a-button
        >
      </template>
      <!-- 状态 -->
      <template #status="{ text: status }">
        <span>
          <Tag :color="status == 0 ? 'success' : 'error'">{{
            proxy.$filters.filterLabelStatus(status)
          }}</Tag>
        </span>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: t('routes.demo.knowledgeManager.labelManagerSet.create'),
              icon: 'ion:add-outline',
              onClick: handleAdd.bind(null, record),
              auth: 'documentManager:tag:create',
            },
            {
              label: t('routes.demo.knowledgeManager.labelManagerSet.edit'),
              icon: 'material-symbols:edit-square-outline',
              onClick: handleEdit.bind(null, record),
              auth: 'documentManager:tag:update',
            },
            {
              label: t('routes.demo.knowledgeManager.labelManagerSet.delete'),
              icon: 'ion:trash-outline',
              popConfirm: {
                title: t('routes.demo.knowledgeManager.labelManagerSet.deleteConfirm'),
                placement: 'top',
                confirm: handleDelete.bind(null, record),
              },
              auth: 'documentManager:tag:delete',
            },
          ]"
        />
      </template>
    </BasicTable>
    <!-- 添加标签弹窗 -->
    <addLabel @register="registerLabelModal" @success="reload()" />
  </div>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getLabelColumns, getLabelFormConfig } from './tableData';
  import addLabel from './addLabel.vue';
  import { useModal } from '/@/components/Modal';
  import { getDocLabelApi, deleteDocLabelApi } from '/@/api/demo/flow';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { Tag } from 'ant-design-vue';
  import { getCurrentInstance } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';

  const { createMessage } = useMessage();
  const { proxy }: any = getCurrentInstance();
  const { t } = useI18n();

  const [registerTable, { reload }] = useTable({
    titleHelpMessage: 'labelManagerIndex',
    columns: getLabelColumns(),
    api: getDocLabelApi,
    useSearchForm: true,
    formConfig: getLabelFormConfig(), // 表单配置
    showTableSetting: true, // 显示表格设置工具
    tableSetting: { fullScreen: true }, // 表格设置工具配置
    isTreeTable: true,
    childrenColumnName: 'sonlabellist',
    bordered: true,
    rowKey: 'labelID',
    actionColumn: {
      width: 300,
      fixed: 'right',
      title: t('common.action'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
  });

  // 新增
  const handleAdd = (record: Recordable) => {
    openLabelModal(true, { record, isUpdate: false });
  };
  // 修改
  const handleEdit = (record: Recordable) => {
    openLabelModal(true, { record, isUpdate: true });
  };
  // 删除
  async function handleDelete(record: Recordable) {
    await deleteDocLabelApi(record.labelID);
    createMessage.success(t('routes.demo.knowledgeManager.labelManagerSet.deleteSuccess'));
    reload();
  }

  // 添加属性弹窗
  const [registerLabelModal, { openModal: openLabelModal }] = useModal();
</script>
